package com.example.smartcity1.mainfragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.smartcity1.R;
import com.example.smartcity1.adapter.NewsHomeAdapter;
import com.example.smartcity1.adapter.NewsTopicAdapter;
import com.example.smartcity1.adapter.ServiceAdapter;
import com.example.smartcity1.bean.BannerBean;
import com.example.smartcity1.bean.NewsBean;
import com.example.smartcity1.bean.NewsTypeBean;
import com.example.smartcity1.bean.ServiceBean;
import com.example.smartcity1.network.RetrofitClient;
import com.example.smartcity1.news.NewsPageActivity;
import com.example.smartcity1.news.NewsSearchActivity;
import com.example.smartcity1.service.illegal.IllegalHomeActivity;
import com.example.smartcity1.service.life.LifeHomeActivity;
import com.example.smartcity1.service.parklot.ParklotListActivity;
import com.example.smartcity1.service.patient.HospitalListActivity;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class HomeFragment extends Fragment {
    private static final String TAG = "HomeFragment";
    private Banner banner;
    private SearchView searchView;
    private GridView gridServiceView;
    private GridView gridNewsTopicView;
    private GridView gridNewsTypeView;
    private ListView listNewsView;

    private List<BannerBean.RowsDTO> mBannerList;
    private List<ServiceBean.RowsDTO> mServiceList;
    private List<NewsBean.RowsDTO> mNewsTopicList;
    private List<NewsTypeBean.DataDTO> mNewsTypeList;

    public HomeFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initView(view);
        Log.d(TAG, "onCreateView: 启动");
        //搜索框
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Intent intent = new Intent(getActivity(), NewsSearchActivity.class);
                startActivity(intent);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        getBannerData();//获取轮播图数据
        setService();//设置推荐服务数据
        setNewsTopic();//设置专题新闻数据
        setNewsType();//设置新闻专栏数据
        setNewsList(36);//设置新闻列表数据
        gridNewsTypeView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                setNewsList(mNewsTypeList.get(position).getDictCode());
            }
        });
        return view;
    }

    //findViewById
    private void initView(View view) {
        searchView = view.findViewById(R.id.searchView);
        banner = view.findViewById(R.id.banner);
        gridNewsTypeView = view.findViewById(R.id.homeGridType);
        listNewsView = view.findViewById(R.id.homeListNews);

    }

    //获取轮播图数据
    private void getBannerData() {
        RetrofitClient.appService().getBannerData().enqueue(new Callback<BannerBean>() {
            @Override
            public void onResponse(Call<BannerBean> call, Response<BannerBean> response) {
                BannerBean bannerBean = response.body();
                if (bannerBean != null && bannerBean.getCode() == 200) {
                    mBannerList = bannerBean.getRows();
                    Log.d(TAG, "onResponse: 获取轮播图数据");
                    useBanner();
                }
            }

            @Override
            public void onFailure(Call<BannerBean> call, Throwable t) {
            }
        });
    }

    //使用轮播图
    private void useBanner() {
        banner.setAdapter(new BannerImageAdapter<BannerBean.RowsDTO>(mBannerList) {
            @Override
            public void onBindView(BannerImageHolder bannerImageHolder, BannerBean.RowsDTO rowsDTO, int i, int i1) {
                final String IPP = IP + mBannerList.get(i).getImgUrl();
                Glide.with(getActivity().getApplicationContext())
                        .load(IP + mBannerList.get(i).getImgUrl())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                        .placeholder(R.mipmap.ic_launcher)
                        .into(bannerImageHolder.imageView);
                Log.d(TAG, "onResponse: " + "使用轮播图: IP = " + IPP);
            }
        })
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(getActivity().getApplicationContext()));
    }

    //设置推荐服务数据
    private void setService() {
        RetrofitClient.appService().getServiceData().enqueue(new Callback<ServiceBean>() {
            @Override
            public void onResponse(Call<ServiceBean> call, Response<ServiceBean> response) {
                ServiceBean serviceBean = response.body();
                if (serviceBean != null && serviceBean.getCode() == 200) {
                    mServiceList = serviceBean.getRows();
                    Collections.reverse(mServiceList);
                    ServiceBean.RowsDTO rowsDTO = new ServiceBean.RowsDTO();
                    rowsDTO.setServiceName("全部服务");
                    rowsDTO.setImgUrl("All");
                    ServiceBean.RowsDTO rowsDTO2 = new ServiceBean.RowsDTO();
                    rowsDTO2.setServiceName("预约检车");
                    rowsDTO2.setImgUrl("");
                    mServiceList.add(rowsDTO);
                    mServiceList.add(rowsDTO2);

                    ServiceAdapter serviceAdapter = new ServiceAdapter(getActivity().getApplicationContext(), R.layout.item_grid_icon, mServiceList);
                    gridServiceView = getActivity().findViewById(R.id.homeGridService);
                    gridServiceView.setAdapter(serviceAdapter);
                    gridServiceView.setOnItemClickListener((parent, view, position, id) -> {
                        switch (position) {
                            case 0:
                                startActivity(new Intent(getActivity(), ParklotListActivity.class));
                                break;
                            case 1:
                                startActivity(new Intent(getActivity(), IllegalHomeActivity.class));
                                break;
                            case 2:
                                startActivity(new Intent(getActivity(), LifeHomeActivity.class));
                                break;
                            case 3:
                                startActivity(new Intent(getActivity(), HospitalListActivity.class));
                                break;
                            case 4:
                                Toast.makeText(getContext(), "4", Toast.LENGTH_SHORT).show();
                                break;
                            case 5:
                                Toast.makeText(getContext(), "5", Toast.LENGTH_SHORT).show();
                                break;
                            case 6:
                                Toast.makeText(getContext(), "6", Toast.LENGTH_SHORT).show();
                                break;
                            case 7:
                                Toast.makeText(getContext(), "7", Toast.LENGTH_SHORT).show();
                                break;
                        }

                    });
                }
            }

            @Override
            public void onFailure(Call<ServiceBean> call, Throwable t) {
            }
        });
    }

    //设置专题新闻数据
    private void setNewsTopic() {
        RetrofitClient.appService().getNewsTopicData().enqueue(new Callback<NewsBean>() {
            @Override
            public void onResponse(Call<NewsBean> call, Response<NewsBean> response) {
                NewsBean newsBean = response.body();
                if (newsBean != null && newsBean.getCode() == 200) {
                    Log.d(TAG, "onResponse: 获取新闻专题数据");
                    mNewsTopicList = newsBean.getRows();
                    NewsTopicAdapter newsTopicAdapter = new NewsTopicAdapter(getActivity().getApplicationContext(), R.layout.item_news_topic, mNewsTopicList);
                    gridNewsTopicView = getActivity().findViewById(R.id.homeGridTopic);
                    gridNewsTopicView.setAdapter(newsTopicAdapter);
                    gridNewsTopicView.setOnItemClickListener((parent, view, position, id) -> {
                        Gson gson = new Gson();
                        NewsBean.RowsDTO data = mNewsTopicList.get(position);
                        String json = gson.toJson(data);
                        Intent intent = new Intent(getContext(), NewsPageActivity.class);
                        intent.putExtra("news",json);
                        startActivity(intent);
                    });
                }
            }

            @Override
            public void onFailure(Call<NewsBean> call, Throwable t) {
            }
        });
    }

    //设置新闻专栏数据
    private void setNewsType() {
        RetrofitClient.appService().getNewsTypeData().enqueue(new Callback<NewsTypeBean>() {
            @Override
            public void onResponse(Call<NewsTypeBean> call, Response<NewsTypeBean> response) {
                NewsTypeBean newsTypeBean = response.body();
                if (newsTypeBean != null && newsTypeBean.getCode() == 200) {
                    Log.d(TAG, "onResponse: 获取新闻专栏数据");
                    mNewsTypeList = newsTypeBean.getData();
                    List<String> data = new ArrayList<>();
                    for (NewsTypeBean.DataDTO i : mNewsTypeList) {
                        data.add(i.getDictLabel());
                    }
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item_news_type, R.id.item_news_type_txt, data);
                    gridNewsTypeView.setAdapter(adapter);

                }
            }

            @Override
            public void onFailure(Call<NewsTypeBean> call, Throwable t) {

            }
        });
    }

    //设置新闻列表数据
    private void setNewsList(int type) {
        RetrofitClient.appService().getNewsHomeData(type).enqueue(new Callback<NewsBean>() {
            @Override
            public void onResponse(Call<NewsBean> call, Response<NewsBean> response) {
                NewsBean newsBean = response.body();
                if (newsBean != null && newsBean.getCode() == 200) {
                    Log.d(TAG, "onResponse: 获取新闻列表数据");
                    List<NewsBean.RowsDTO> mNewsList = newsBean.getRows();
                    NewsHomeAdapter newsHomeAdapter = new NewsHomeAdapter(getActivity().getApplicationContext(), R.layout.item_news_home, mNewsList);
                    listNewsView.setAdapter(newsHomeAdapter);
                    listNewsView.setOnItemClickListener((parent, view, position, id) -> {
                        Gson gson = new Gson();
                        NewsBean.RowsDTO data = mNewsList.get(position);
                        String json = gson.toJson(data);
                        Intent intent = new Intent(getContext(), NewsPageActivity.class);
                        intent.putExtra("news",json);
                        startActivity(intent);
                    });
                }
            }

            @Override
            public void onFailure(Call<NewsBean> call, Throwable t) {

            }
        });
    }
}